package client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cview extends JFrame {
    private static Cview myfram=null;
    public static Cview getMyfram(){
        if(myfram==null)
            myfram=new Cview();
        return myfram;
    }
    public static Font dfon1=new Font("",20,20);
    public static Font dfon2=new Font("",20,15);
    public static Font dfon3=new Font("", 20, 12);
    public static Color dfpurple1=new Color(150,50,200);
    public static Color dfpurple2=new Color(120,50,180);
    public static Color dfblue1=new Color(60,60,150);
    public static Color dfblue2=new Color(100,140,200);//按钮一般用这个
    public static Dimension ddms6=new Dimension(30,15);

    private Cview(){
        //this.add(rstcol);//this.add(new menu(), BorderLayout.WEST);
        //
        this.getContentPane().add(new login(), BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(625, 530);
        this.setVisible(true);
        //this.repaint();
    }
    class login extends JPanel{
        JLabel msg=new JLabel("LOG IN",SwingConstants.CENTER);
        JTextField uname=new JTextField("");
        JTextField upwd=new JTextField("");
        JButton submit=new JButton("SUBMIT");
        JPanel lgpanel=new JPanel();//信息输入栏，位于东边
        String bggpath="rsc/image/database_login.png"; ImageIcon bggicon=new ImageIcon(bggpath);
        JLabel bgg=new JLabel(bggicon);
        private login(){
            this.add(bgg, BorderLayout.CENTER);

            lgpanel.setLayout(new GridLayout(20,1));
            msg.setFont(dfon1);
            uname.setFont(dfon3); uname.setSize(ddms6);
            upwd.setFont(dfon3); upwd.setSize(ddms6);
            submit.setFont(dfon3); submit.setSize(ddms6); submit.setBackground(dfblue2);
            submit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(uname.getText()+" "+upwd.getText());//可以了
                    Cmodel.setcId(Integer.parseInt("0"+uname.getText()));
                    //login2menu();
                    Ccontrol.getCtller().enterAttempt(upwd.getText());
                }
            });
            //
            lgpanel.add(new JLabel());lgpanel.add(new JLabel());lgpanel.add(new JLabel());
            lgpanel.add(new JLabel());lgpanel.add(new JLabel());
            //占位，本身没有意义
            lgpanel.add(msg);
            lgpanel.add(new JPanel(){
                {
                    this.setBackground(dfpurple1);
                    this.setLayout(new GridLayout(1,2));
                    this.add(new JLabel("username:",SwingConstants.CENTER){
                        {
                            this.setFont(dfon2);
                        }
                    });
                    this.add(uname);
                }
            });
            lgpanel.add(new JPanel(){
                {
                    this.setBackground(dfpurple1);
                    this.setLayout(new GridLayout(1,2));
                    this.add(new JLabel("password:",SwingConstants.CENTER){
                        {
                            this.setFont(dfon2);
                        }
                    });
                    this.add(upwd);
                }
            });
            lgpanel.add(new JPanel(){
                {
                    this.setBackground(dfpurple1);
                    //this.setLayout(new GridLayout(1,2));
                    this.add(submit,BorderLayout.CENTER);
                }
            });
            lgpanel.setBackground(dfpurple2);
            lgpanel.setPreferredSize(new Dimension(200,500));
            this.add(lgpanel, BorderLayout.EAST);
            this.setBackground(dfblue1);
        }
    }
    class menu extends JPanel{
        private menu(){
            this.setLayout(new GridLayout(9,1));
            this.add(new JLabel());//占位
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    //this.setLayout(new GridLayout(1,2));
                    this.add(new JButton("Colleges"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("CollegesButton is pressed.");
                                    //TODO 按下后加载college页面
                                    Ccontrol.getCtller().settabl("college0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("Majors"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("MajorsButton is pressed.");
                                    //TODO 按下后加载major页面
                                    Ccontrol.getCtller().settabl("major0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("Teachers"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("TeachersButton is pressed.");
                                    //TODO 按下后加载teacher页面
                                    Ccontrol.getCtller().settabl("teacher0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("Students"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("StudentsButton is pressed.");
                                    //TODO 按下后加载student页面
                                    Ccontrol.getCtller().settabl("student0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("Courses"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("CoursesButton is pressed.");
                                    //TODO 按下后加载course页面
                                    Ccontrol.getCtller().settabl("course0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("Attendclass0"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("AttendButton is pressed.");
                                    //TODO 按下后加载course页面
                                    Ccontrol.getCtller().settabl("attendclass0 ");
                                }
                            });
                        }
                    });
                }
            });
            this.setBackground(dfpurple2);
        }
    }
    class operate extends JPanel{
        private JTextField cdtcommand=new JTextField(" stuId=2212537");
        private operate(){
            this.setLayout(new GridLayout(10,1));
            this.add(new JLabel(Cmodel.getMe().getIdentity()));
//            this.add(new JPanel(){
//                {
//                    this.setBackground(dfpurple2);
//                    this.add(new JButton("insert"){
//                        {
//                            this.setBackground(dfblue2);
//                            this.addActionListener(new ActionListener() {
//                                @Override
//                                public void actionPerformed(ActionEvent e) {
//                                    //System.out.println("insertButton is pressed.");
//                                    //TODO 按下后进行数据库插入操作
//                                    Ccontrol.getCtller().setAct("insert into ");
//                                }
//                            });
//                        }
//                    });
//                }
//            });
//            this.add(new JPanel(){
//                {
//                    this.setBackground(dfpurple2);
//                    this.add(new JButton("delete"){
//                        {
//                            this.setBackground(dfblue2);
//                            this.addActionListener(new ActionListener() {
//                                @Override
//                                public void actionPerformed(ActionEvent e) {
//                                    //System.out.println("deleteButton is pressed.");
//                                    //TODO 按下后加载删除操作
//                                    Ccontrol.getCtller().setAct("delete ");
//                                }
//                            });
//                        }
//                    });
//                }
//            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("select"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("selectButton is pressed.");
                                    //TODO 按下后加载查找操作
                                    Ccontrol.getCtller().setAct("select * from ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("update"){
                        {
                            this.setBackground(dfblue2);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("updateButton is pressed.");
                                    //TODO 按下后加载更新操作
                                    Ccontrol.getCtller().setAct("update ");
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JLabel());//占位
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("CONFIRM"){
                        {
                            this.setBackground(dfblue2);
                            this.setFont(dfon3);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("confirmButton is pressed.");
                                    //TODO 按下后加载确认操作
                                    Ccontrol.getCtller().execute();
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JPanel(){
                {
                    this.setBackground(dfpurple2);
                    this.add(new JButton("EXIT"){
                        {
                            this.setBackground(dfblue2);
                            this.setFont(dfon3);
                            this.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    //System.out.println("exitButton is pressed.");
                                    //TODO 按下后加载退出操作
                                    menu2login();
                                }
                            });
                        }
                    });
                }
            });
            this.add(new JLabel());//占位
            cdtcommand.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //System.out.println(cdtcommand.getText());
                    Ccontrol.getCtller().setCondition(cdtcommand.getText());
                }
            });
            this.add(cdtcommand);

            this.setBackground(dfpurple1);
        }
    }
    class directQuery extends JPanel{
        private JTextField dcommand=new JTextField();
        private directQuery(){
            this.setLayout(new GridLayout(1,2));
            dcommand.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("QueryEnter is pressed.");
                    //TODO 按下后加载query
                    doQuery(dcommand.getText());
                }
            });
            dcommand.setPreferredSize(new Dimension(400,30));
            this.add(dcommand);
            this.add(new JButton("DoQuery"){
                {
                    this.setBackground(dfblue2);
                    this.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.out.println("DirectQueryButton is pressed.");
                            //TODO 按下后加载query
                            doQuery(dcommand.getText());
                        }
                    });
                }
            });
        }
        private void doQuery(String command){
            Ccontrol.getCtller().directCommand(command);
        }
    }

    public void login2menu(){
        this.getContentPane().removeAll();//是否必须getPanel才能真正remove
        //目前无法直接刷新得到新的panel，问题到也不大
        this.getContentPane().add(new menu(), BorderLayout.WEST);

        this.getContentPane().add(Cvdisplay.getPanel(Cmodel.getMe().getRst()), BorderLayout.CENTER);
        this.getContentPane().add(new operate(), BorderLayout.EAST);
        if(Cmodel.getMe().getIdentity().equals("dba")){
            this.getContentPane().add(new directQuery(),BorderLayout.SOUTH);
        }
    }
    private void menu2login(){
        this.getContentPane().removeAll();//是否必须getPanel才能真正remove
        this.getContentPane().add(new login(), BorderLayout.CENTER);
    }

//    public void paint(Graphics g) {
//        super.paint(g);//调用父类方法
//        Color c = g.getColor();
//        g.setColor(Color.RED);
//        g.fillOval(x, y, 30, 30);
//        g.setColor(c);
//    }

    public static void main(String args[]){
        Cview.getMyfram();
    }
}
